package entities;

import interfaze.SpaceInvadersGamingPanel;

import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import logic.Utilities;

@SuppressWarnings("serial")
public class Defender extends MovableEntity {

	private ImageIcon icon;
	public int hits=0;
	public int velProyectil=50;
	public int tRecarga=1000;
	
	/**
	 * creates a new Defender at a specified location in a specified panel with a specified scale
	 * @param x
	 * @param y coordinates
	 * @param xScale
	 * @param yScale scale
	 * @param p panel to which the Defender belongs, useful to access window width and height attributes when needed
	 */
	public Defender(int x, int y, float xScale, float yScale, SpaceInvadersGamingPanel p) {
		super(x, y);
		Image image = Utilities.loadImage("res/Player.png");
		if(xScale != 1 || yScale != 1) {
			image = Utilities.resizeImage(xScale, yScale, "res/Player.png");
		}
		icon = new ImageIcon(image);
		this.setIcon(icon);
		this.width = image.getWidth(null);
		this.height = image.getHeight(null);
		this.setBounds((int)this.x, (int)this.y, this.width, this.height);
		lastUpdate=System.currentTimeMillis();
		this.p=p;
	}
	
	public Defender(int x, int y, int dx, int dy, float xScale, float yScale) {
		super(x, y, dx, dy);
		Image image = Utilities.loadImage("res/Player.png");
		if(xScale != 1 || yScale != 1) {
			image = Utilities.resizeImage(xScale, yScale, "res/Player.png");
		}
		icon = new ImageIcon(image);
		this.setIcon(icon);
		this.width = image.getWidth(null);
		this.height = image.getHeight(null);
		this.setBounds((int)this.x, (int)this.y, this.width, this.height);
	}
	
	public static int getWidth(float xScale) {
		return Math.round(57 * xScale);
	}
	
	public static int getHeight(float yScale) {
		return Math.round(75 * yScale);
	}
	/**
	 * decreases the reload time, or increases the amount of shots you can take per fraction of time, different interpretations, same result
	 */
	public void incRelaod(){
		tRecarga=tRecarga/2;
	}
	/**
	 * increases the speed at which the Defender's shots travel through the screen
	 */
	public void incSpeed(){
		if(velProyectil<1600){
		velProyectil=velProyectil*2;
		}
	}
	/**
	 * creates a DefenderShot and stores it in its according list
	 * @param lista the list of DefenderShots currently on screen
	 */
	public void shoot(ArrayList<DefenderShot> lista){
		DefenderShot shot=new DefenderShot((int)this.x+this.getWidth()/2, (int)this.y, 0, -velProyectil, p);
		lista.add(shot);
		p.add(shot,0);
	}
}
